package com.fengye.school.model.school;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fengye.school.model.BaseEntity;
import com.fengye.school.model.school.enums.GradingType;
import com.fengye.school.model.school.enums.SubjectLanguageType;
import com.fengye.school.model.school.subject.SubjectBooks;
import com.fengye.school.model.school.subject.SubjectRequirement;
import com.fengye.school.model.school.subject.SubjectSchedule;
import lombok.Getter;
import lombok.Setter;
import org.springframework.scheduling.annotation.Scheduled;

import javax.persistence.*;
import java.util.Set;


/**
 * 课纲
 */
@Table(name = "tb_s_subject")
@org.hibernate.annotations.Table(appliesTo = "tb_s_subject", comment = "课纲表")
@Entity
@Getter
@Setter
public class Subject extends BaseEntity {


    /**
     * 课程代码:MI131
     */
    @Column(name = "code", columnDefinition = "varchar(255) comment '课程代码'")
    private String code;

    /**
     * 课时: 40学时
     */
    @Column(name = "hours", columnDefinition = "varchar(255) comment '课时'")
    private String hours;

    /**
     * 学分: 2学分
     */
    @Column(name = "`credits`", columnDefinition = "varchar(255) comment '学分'")
    private int credits;

    /**
     * 课程名称: (中文)专业实习
     */
    @Column(name = "title_zh", columnDefinition = "varchar(255) comment '中文名称'")
    private String titleZh;

    /**
     * 英文名称 Speciality Practice
     */
    @Column(name = "title_en", columnDefinition = "varchar(255) comment '英文名称'")
    private String titleEn;




    /**
     * 授课语言 中文
     */
    @Column(name = "language", columnDefinition = "varchar(255) comment '授课语言'")
    private SubjectLanguageType language = SubjectLanguageType.CHINESE;


    /**
     * 先修课程
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "t_subject_pre_subject",
    joinColumns = {@JoinColumn(name = "subject_id", columnDefinition = "bigint comment '科目id'")},
            inverseJoinColumns = {@JoinColumn(name = "pre_subject_id", columnDefinition = "bigint comment '先修科目id'")}
    )
    @JsonIgnore
    private Set<Subject> preSubjectSet;

    @ManyToMany(mappedBy = "preSubjectSet", targetEntity = Subject.class)
    @JsonIgnore
    private Set<Subject> nextSubjectSet;
    /**
     * 开课院系
     */
    @ManyToOne
    @JoinColumn(name = "school_id", columnDefinition = "bigint comment '学院id'")
    @JsonIgnore
    private School school;

    /**
     * 授课教师
     */
    @ManyToMany
    @JoinTable(name = "t_subject_teacher",
            joinColumns = {@JoinColumn(name = "school_id", columnDefinition = "bigint comment '科目id'")},
        inverseJoinColumns = {@JoinColumn(name = "teacher_id", columnDefinition = "bigint comment '教师id'")}
    )
    private Set<Teacher> teacherSet;

    /**
     * 课程网址
     */
    @Column(name = "web_page", columnDefinition = "varchar(255) comment '课程网址'")
    private String webPage;


    @Column(name = "short_name", columnDefinition = "varchar(255) comment '简称'")
    private String shortName;

    @Column(name = "sketch", columnDefinition = "varchar(255) comment '简述'")
    private String sketch;

    @Column(name = "`description_zh`", columnDefinition = "varchar(255) comment '科目中文说明'")
    private String descriptionZh;

    /**
     * 英文简介
     */
    @Column(name = "`description_en`", columnDefinition = "varchar(255) comment '科目英文说明'")
    private String descriptionEn;

    /**
     * 学习目标
     */
    @Column(name = "`target`", columnDefinition = "varchar(255) comment '科目学习目标'")
    private String target;

    /**
     * 教学内容/进度安排/要求
     * schedule,requirement
     */
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "subject", targetEntity = SubjectSchedule.class)
    private Set<Scheduled> schedule;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "subject", targetEntity = SubjectRequirement.class)
    private Set<SubjectRequirement> requirementSet;

    /**
     * 考核方式
     */
    @Column(name = "grading_type", columnDefinition = "varchar(255) comment '考核方式'")
    private GradingType grading;

    /**
     * 教材/参考资料
     */
    @OneToMany(mappedBy = "subject", targetEntity = SubjectBooks.class, fetch = FetchType.EAGER)
    private Set<SubjectBooks> subjectBooksSet;

    /**
     * 其他
     */
    @Column(name = "`more`", columnDefinition = "varchar(255) comment '其他'")
    private String more;

    /**
     * 备注
     */
    @Column(name = "`notes`", columnDefinition = "varchar(255) comment '备注'")
    private String notes;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Subject)) return false;
        if (!super.equals(o)) return false;

        Subject subject = (Subject) o;

        return getCode().equals(subject.getCode());
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + getCode().hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "Subject{" +
                "code='" + code + '\'' +
                ", hours='" + hours + '\'' +
                ", credits=" + credits +
                ", titleZh='" + titleZh + '\'' +
                ", titleEn='" + titleEn + '\'' +
                ", language=" + language +
                ", preSubjectSet=" + preSubjectSet +
                ", nextSubjectSet=" + nextSubjectSet +
                ", school=" + school +
                ", teacherSet=" + teacherSet +
                ", webPage='" + webPage + '\'' +
                ", shortName='" + shortName + '\'' +
                ", sketch='" + sketch + '\'' +
                ", descriptionZh='" + descriptionZh + '\'' +
                ", descriptionEn='" + descriptionEn + '\'' +
                ", target='" + target + '\'' +
                ", schedule=" + schedule +
                ", requirementSet=" + requirementSet +
                ", grading=" + grading +
                ", subjectBooksSet=" + subjectBooksSet +
                ", more='" + more + '\'' +
                ", notes='" + notes + '\'' +
                '}';
    }
}
